import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/',
    component: () => import('../views/Layout.vue'),
    children: [
      { path: '', redirect: '/dashboard' },
      { path: 'dashboard', component: () => import('../views/Dashboard.vue') },
      { path: 'seting', component: () => import('../views/Settings.vue') },
      { path: 'project/list', component: () => import('../views/project/ProjectList.vue') },
      { path: 'project/create', component: () => import('../views/project/ProjectCreate.vue') },
      // 项目功能页面路由
      { path: 'project/info/:id?', component: () => import('../views/project/ProjectInfo.vue') },
      { path: 'project/stakeholder/:id?', component: () => import('../views/stakeholder/StakeholderManage.vue') },
      { path: 'project/status/:id?', component: () => import('../views/project/ProjectStatus.vue') },
      { path: 'project/timeline/:id?', component: () => import('../views/project/ProjectTimeline.vue') },
      { path: 'project/change/:id?', component: () => import('../views/project/ChangeRecord.vue') },
      { path: 'project/milestone/:id?', component: () => import('../views/project/MilestonePlan.vue') },
      { path: 'project/experience/:id?', component: () => import('../views/project/ExperienceSummary.vue') },
      { path: 'project/human/:id?', component: () => import('../views/project/HumanResource.vue') },
      { path: 'project/issue/:id?', component: () => import('../views/project/IssueSummary.vue') },
      { path: 'project/document/:id?', component: () => import('../views/project/DocumentManage.vue') },
      { path: 'project/weight/:id?', component: () => import('../views/project/WeightSetting.vue') },
      { path: 'project/equipment/:id?', component: () => import('../views/project/ProjectEquipmentManagement.vue') },
      { path: 'project/biweekly-report/:id?', component: () => import('../views/project/BiweeklyReport.vue') },
      { path: 'report/my', component: () => import('../views/report/MyReport.vue') },
      { path: 'report/team', component: () => import('../views/report/TeamReport.vue') },
      { path: 'profile', component: () => import('../views/Profile.vue') },
      { path: 'user/manage', component: () => import('../views/UserManage.vue') },
      { path: 'user/project', component: () => import('../views/UserProjectManage.vue') },
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫：未登录跳转到 /login
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  
  // If no token or the token has expired, redirect to login, but not if already at login page
  if (!token) {
    if (to.path !== '/login') {
      next('/login');  // Only redirect to /login if not already on it
    } else {
      next();  // If already on the login page, just allow the navigation
    }
  }
  const user = localStorage.getItem('user')
  if (!user && to.path !== '/login') {
    next('/login')
  } else if (user && to.path === '/login') {
    next('/')
  } else {
    next()
  }
})


export default router 